package com.server.user.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.server.model.all.constant.redis.UserBlackListKeyConstant;
import com.server.model.all.enums.YesNoEnum;
import com.server.model.all.user.bo.AddUserBlackListBo;
import com.server.model.all.user.bo.EnableDisableUserBlackListBo;
import com.server.model.all.user.bo.RemoveUserBlackListBo;
import com.server.model.all.user.entity.UserBlackListEntity;
import com.server.user.mapper.UserBlackListMapper;
import com.server.user.service.UserBlackListService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class UserBlackListServiceImpl extends ServiceImpl<UserBlackListMapper, UserBlackListEntity> implements UserBlackListService {

    @Resource
    private RedisTemplate<String, String> redisTemplate;

    /**
     * 添加用户-黑名单
     * @author LXC
     * @date 2023/2/2 9:28
     */
    @Override
    public boolean addUserBlackList(List<AddUserBlackListBo> addUserBlackListBoList) {
        List<UserBlackListEntity> userBlackListEntityList = new ArrayList<>();

        List<Integer> blackUserIdList = addUserBlackListBoList.stream().map(AddUserBlackListBo::getBlackUserId).collect(Collectors.toList());

        List<UserBlackListEntity> blackUserIdListNew = list(new LambdaQueryWrapper<UserBlackListEntity>()
                .select(UserBlackListEntity::getBlackUserId)
                .in(UserBlackListEntity::getBlackUserId, blackUserIdList)
                .eq(UserBlackListEntity::getDelFlag, YesNoEnum.NO.getCode())
        );

        for (AddUserBlackListBo addUserBlackListBo:addUserBlackListBoList){
            UserBlackListEntity userBlackListEntity = BeanUtil.copyProperties(addUserBlackListBo, UserBlackListEntity.class);

            UserBlackListEntity userBlackList = blackUserIdListNew.stream()
                    .filter(blackListEntity -> blackListEntity.getBlackUserId().equals(userBlackListEntity.getBlackUserId()))
                    .findFirst()
                    .orElse(null);

            if(!ObjectUtils.isEmpty(userBlackList)){
                continue;
            }

            userBlackListEntityList.add(userBlackListEntity);

            redisTemplate.opsForSet().add(UserBlackListKeyConstant.USER_BLACK_LIST,String.valueOf(userBlackListEntity.getBlackUserId()));
        }

        return saveBatch(userBlackListEntityList);
    }

    /**
     * 移除用户黑名单
     * @author LXC
     * @date 2023/2/2 10:00
     */
    @Override
    public boolean removeUserBlackList(List<RemoveUserBlackListBo> removeUserBlackListBoList) {
        for (RemoveUserBlackListBo removeBlackWhiteListBo:removeUserBlackListBoList){
            UserBlackListEntity userBlackListEntity = BeanUtil.copyProperties(removeBlackWhiteListBo, UserBlackListEntity.class);
            userBlackListEntity.setDelFlag(YesNoEnum.YES.getCode());

            update(userBlackListEntity,new LambdaQueryWrapper<UserBlackListEntity>()
                    .eq(UserBlackListEntity::getUserBlackListId,removeBlackWhiteListBo.getIds())
                    .eq(UserBlackListEntity::getDelFlag,YesNoEnum.NO.getCode())
            );

            redisTemplate.opsForSet().remove(UserBlackListKeyConstant.USER_BLACK_LIST,String.valueOf(userBlackListEntity.getBlackUserId()));
        }

        return true;
    }

    /**
     * 启用禁用-用户-黑名单
     * @author LXC
     * @date 2023/2/2 11:51
     */
    @Override
    public boolean enableDisableUserBlackList(List<EnableDisableUserBlackListBo> enableDisableUserBlackListBoList) {
        for (EnableDisableUserBlackListBo enableDisableUserBlackListBo:enableDisableUserBlackListBoList){
            UserBlackListEntity userBlackListEntity = BeanUtil.copyProperties(enableDisableUserBlackListBo, UserBlackListEntity.class);

            update(userBlackListEntity,new LambdaQueryWrapper<UserBlackListEntity>()
                    .eq(UserBlackListEntity::getUserBlackListId,enableDisableUserBlackListBo.getIds())
                    .eq(UserBlackListEntity::getDelFlag,YesNoEnum.NO.getCode())
            );

            if(YesNoEnum.YES.getCode().equals(userBlackListEntity.getStatus())){
                redisTemplate.opsForSet().add(UserBlackListKeyConstant.USER_BLACK_LIST,String.valueOf(userBlackListEntity.getBlackUserId()));
                continue;
            }

            if(YesNoEnum.NO.getCode().equals(userBlackListEntity.getStatus())){
                redisTemplate.opsForSet().remove(UserBlackListKeyConstant.USER_BLACK_LIST,String.valueOf(userBlackListEntity.getBlackUserId()));
            }

        }
        return true;
    }


}
